package com.zds.passwordbox;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.zds.passwordbox.common.Constant;


public class GlobalApplication extends Application {

    // 当前处于活动的activity
    private int countActivity = 0;

    // 当前是否是处于后台状态(初始为true，表示规定应用首次进入前也处于后台)
    private volatile boolean isBackground = true;

    private boolean isFromFilePicker = false;

    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                countActivity++;
                if (countActivity == 1 && isBackground) {
                    //说明应用重新进入了前台（不包括首次启动）
                    loginCheck(activity);
                    isBackground = false;
                } else {
                    boolean isVerified = getSharedPreferences(Constant.IS_VERIFIED,
                            Context.MODE_PRIVATE).getBoolean(Constant.IS_VERIFIED, false);
                    if (!isVerified && !activity.getClass().equals(LoginActivity.class)) {
                        Intent setIntent = new Intent(Intent.ACTION_MAIN);
                        setIntent.addCategory(Intent.CATEGORY_HOME);
                        setIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        activity.startActivity(setIntent);
                    }
                }
                // 此变量用过之后，立即变为false
                isFromFilePicker = false;
            }

            private void loginCheck(Activity activity) {
                // 如果是从文件选择器重新回到应用，就跳过验证
                if (isFromFilePicker) {
                    // 这是从文件选择器返回，不进行指纹验证
                    return;
                }
                if (!activity.getClass().equals(LoginActivity.class)) {
                    // 当MainActivity等启动时，需要验证
                    Intent intent1 = new Intent(activity, LoginActivity.class);
                    //intent.putExtra("backgroundFlag", isBackground);
                    activity.startActivity(intent1);
                    //activity.finish();
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
                countActivity--;
                if (countActivity <= 0 && !isBackground) {
                    //说明应用进入了后台
                    Toast.makeText(GlobalApplication.this, "密码盒子进入后台", Toast.LENGTH_SHORT).show();
                    isBackground = true;
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    public void setFromFilePicker(boolean fromFilePicker) {
        isFromFilePicker = fromFilePicker;
    }
}